package logic.strategy;

import java.util.Map;
import java.util.HashMap;

import logic.*;

public class Cluster {
	//===	Fields	===
	private Map<String, Country>	countries;
	private	Player					owner;	
	
	//=== Constructors ===
	public Cluster(Player owner)
	{
		this.owner = owner;
		countries = new HashMap<String, Country>();
	}
	//=== Methods ===
	public boolean hasCountry(Country country)
	{
		return countries.containsKey(country.getName());
	}
	
	public void add(Country country)
	{
		countries.put(country.getName(), country);
	}
	public void flush()
	{
		countries.clear();
	}
	
	/**
	 * 
	 * @param country
	 * Create the cluster from given country
	 */
	public void initFromCountry(Country country)
	{
		flush();
		add(country);
		dfsVisit(country);
	}
	private void dfsVisit(Country country)
	{
		for(Country neighbour : country.getNeighbors().values())
		{
			if(!hasCountry(neighbour) && neighbour.getOwner().getPlayerName().compareTo(owner.getPlayerName()) == 0)
			{
				add(neighbour);
				dfsVisit(neighbour);
			}
		}
	}
	
	public int getStrength()
	{
		int res = 0;
		for(Country country: countries.values())
		{
			res += country.getSoldiers();
		}
		return res;
	}
	
}
